package com.wanxi.chat.messagehandler.chat;

import com.wanxi.chat.dispatcher.MessageHandler;

import com.wanxi.chat.message.chat.ChatSendResponse;
import com.wanxi.chat.model.ChatEntity;
import com.wanxi.chat.mongo.api.MongoFeignClient;
import com.wanxi.chat.offline.api.OfflineFeignClient;
import com.wanxi.chat.vo.ChatQueryVO;
import io.netty.channel.Channel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class MessageResponseHandler implements MessageHandler<ChatSendResponse> {

    @Autowired
    private OfflineFeignClient offlineFeignClient;

    @Autowired
    private MongoFeignClient mongoFeignClient;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void execute(Channel channel, ChatSendResponse message) {

        ChatEntity chatEntity = new ChatEntity();
        chatEntity.setId(message.getUserId());
        ChatQueryVO chatQueryVO = new ChatQueryVO();
        chatQueryVO.setToId(message.getUserId());


        mongoFeignClient.mongoUpdateFeign(chatQueryVO);
////        chatRepository.update(chatQueryVO);
        logger.info("[用户:{}消息已读取，数据库状态已修改]", message.getUserId());
        //调用离线服务的remove方法
//        offLineService.remove(chatEntity);
        offlineFeignClient.redisRemoveFeign(chatEntity);
        logger.info("[用户:{}消息已读取，缓存信息已删除]", message.getUserId());

    }

    @Override
    public String getType() {
        return ChatSendResponse.TYPE;
    }
}
